using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

using FAQComponents;
using FAQComponents.Objects;
using FAQComponents.API;

namespace Adge.Web.Faq.Admin
{
	/// <summary>
	/// Summary description for MaintainFAQs.
	/// </summary>
	public partial class MaintainFAQs : System.Web.UI.Page
	{
		private int totalFAQs = -1;
	
		protected void Page_Load(object sender, System.EventArgs e)
		{
			if (!Page.IsPostBack)
			{
				User u = FAQContext.GetContext().LoggedOnUser;
				u.Permissions.Sort();
				category.DataSource = PermissionAPI.GetFAQEditorPermissionsOnly(u.Permissions);
				category.DataBind();

				DisplayFAQsInCategory();
			}
		}

		#region Web Form Designer generated code
		override protected void OnInit(EventArgs e)
		{
			//
			// CODEGEN: This call is required by the ASP.NET Web Form Designer.
			//
			InitializeComponent();
			base.OnInit(e);
		}
		
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{    
			this.dgFAQs.ItemCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler(this.dgFAQs_ItemCommand);
			this.dgFAQs.DeleteCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler(this.dgFAQs_DeleteCommand);
			this.dgFAQs.ItemDataBound += new System.Web.UI.WebControls.DataGridItemEventHandler(this.dgFAQs_ItemDataBound);

		}
		#endregion

		protected void category_SelectedIndexChanged(object sender, System.EventArgs e)
		{
			DisplayFAQsInCategory();
		}

		private void DisplayFAQsInCategory()
		{
			if (category.Items.Count > 0)
			{
				FAQCollection faqs = FAQAPI.GetFAQsInCategory(InvariantString.ToInt32(category.SelectedValue), true, false);
				totalFAQs = faqs.Count;

				dgFAQs.DataSource = faqs;
				dgFAQs.DataBind();

				dgFAQs.Visible = dgFAQs.Items.Count > 0;
				noFAQsMessage.Visible = dgFAQs.Items.Count == 0;
			}
		}

		#region dgFAQs DataGrid Event Handlers
		private void dgFAQs_DeleteCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
		{
			int FAQID = (int) dgFAQs.DataKeys[e.Item.ItemIndex];
			FAQAPI.DeleteFAQ(FAQID);

			DisplayFAQsInCategory();
		}

		private void dgFAQs_ItemCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
		{
			int FAQID = (int) dgFAQs.DataKeys[e.Item.ItemIndex];

			switch (e.CommandName)
			{
				case "MoveUp":
					FAQAPI.MoveFAQSortOrderUp(FAQID);
					DisplayFAQsInCategory();
					break;

				case "MoveDown":
					FAQAPI.MoveFAQSortOrderDown(FAQID);
					DisplayFAQsInCategory();
					break;
			}
		}

		private void dgFAQs_ItemDataBound(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)
		{
			if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
			{
				// add a confirm dialog to the delete button
				LinkButton db = (LinkButton) e.Item.Cells[2].Controls[0];
				db.Attributes["onclick"] = "javascript:if (!confirm('This will permanently delete this FAQ and all FAQ parts within.  Are you sre you want to do this?')) return false; else return confirm('Are you really sure?  I mean, 100%, absolutely, positively sure?');";

				// show/hide the moveUp/moveDown buttons accordingly
				ImageButton moveUp = (ImageButton) e.Item.FindControl("btnMoveUp");
				ImageButton moveDown = (ImageButton) e.Item.FindControl("btnMoveDown");

				moveUp.Visible = e.Item.ItemIndex != 0;
				moveDown.Visible = e.Item.ItemIndex != (totalFAQs - 1);
			}
		}
		#endregion

	}
}
